//
//  XMBagListViewController.m
//  XunMi
//
//  Created by MacBook on 2024/2/29.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMBagListViewController.h"
#import "XMDiscountsModel.h"
#import "XMCardBagListTableViewCell.h"
#import "XMRechargeViewController.h"
#import "XMPointsViewController.h"

@interface XMBagListViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <XMDiscountsModel *> *dataArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, copy) NSString *prevIndex;

@end


@implementation XMBagListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
    [self cardListData];
    [self setupRefresh];
}

#pragma mark - 初始化变量
- (void)initVariables {
    self.page = 1;
    self.prevIndex = @"0";
    self.dataArray = [NSMutableArray array];
}

#pragma mark - refresh
- (void)setupRefresh {
    XMWeakSelf(self);
    self.tableView.mj_header = [XMRefreshNormalHeader headerWithRefreshingBlock:^{
        XMStrongSelf(self);
        [self cardListDataRefresh];
    }];
    
    XMRefreshBackFooter *footer = [XMRefreshBackFooter footerWithRefreshingBlock:^{
        XMStrongSelf(self);
        self.page += 1;
        [self cardListData];
    }];
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
}

- (void)cardListDataRefresh {
    self.page = 1;
    self.prevIndex = @"0";
    [self.dataArray removeAllObjects];
    [self cardListData];
}

- (void)cardListData {
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"page_no": @(self.page),
        @"page_size": @(10),
        @"page_index": self.prevIndex,
        @"module": @(self.type)
    }.mutableCopy;
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/user/coupon" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        [self.tableView hideStatusView];
        self.prevIndex = tip.prevIndex;
        if (tip.success) {
            [MBProgressHUD hideHUD];
            NSArray *record = [result safe_objectForKey:@"record"];
            NSArray *array = [XMDiscountsModel mj_objectArrayWithKeyValuesArray:record];
            [self.dataArray addObjectsFromArray:array];
            if (self.dataArray.count == 0) {
                [self.tableView showStatusView:(YHViewStatusTypeEmptyData)];
                self.tableView.statusTitleLabel.text = @"您还没有卡卷哦～";
            }
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_footer.hidden = self.dataArray.count == 0;
        if (tip.loadMore) {
            [self.tableView.mj_footer endRefreshing];
        } else {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

#pragma mark - setupSubviews
- (void)setupSubviews {
    self.view.backgroundColor = kHEX(@"#F2F2F2");
    self.tableView.backgroundColor = kHEX(@"#F2F2F2");
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:kNibWithName(kStringFromClass(XMCardBagListTableViewCell)) forCellReuseIdentifier:kStringFromClass(XMCardBagListTableViewCell)];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}

#pragma mark - UITableViewDataSource & UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMCardBagListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kStringFromClass(XMCardBagListTableViewCell)];
    if (self.dataArray.count > 0) {
        [cell setupCellWithModel:self.dataArray[indexPath.row] type:self.type];
    }
    
    XMWeakSelf(self);
    cell.useBlock = ^(XMDiscountsModel * _Nonnull model) {
        XMStrongSelf(self);
        switch (model.xmModule) {
            case XMDiscountsChat:
            case XMDiscountsAudio:
            case XMDiscountsVideo:
            {
                [self selectedTabIndex:2];
            } break;
                
            case XMDiscountsRecharge: {
                XMRechargeViewController *vc = [[XMRechargeViewController alloc] initWithNibName:kStringFromClass(XMRechargeViewController) bundle:nil];
                [self.navigationController pushViewController:vc animated:YES];
            } break;
                
            case XMDiscountsWithdraw: {
                XMPointsViewController *vc = [[XMPointsViewController alloc] init];
                [self.navigationController pushViewController:vc animated:YES];
            } break;
                
            default:
                break;
        }
    };
    
    return cell;
}

- (void)selectedTabIndex:(NSInteger)index {
    UITabBarController *tabBarController = [UIViewController currentViewController].tabBarController;
    if (tabBarController.viewControllers.count <= index) {
        return;
    }
    UINavigationController *navigationController = [UIViewController currentNavigatonController];
    if (tabBarController.selectedIndex != index) {
        [tabBarController setSelectedIndex:index];
    }
    [navigationController popToRootViewControllerAnimated:NO];
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

#pragma mark - JXCategoryListContentViewDelegate
- (UIView *)listView {
    return self.view;
}

#pragma mark - Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            _tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0);
        }
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = kHEX(@"F2F2F2");
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _tableView.estimatedRowHeight = CGFLOAT_MIN;
        _tableView.estimatedSectionFooterHeight = CGFLOAT_MIN;
        _tableView.estimatedSectionHeaderHeight = CGFLOAT_MIN;
        _tableView.rowHeight = UITableViewAutomaticDimension;
    }
    return _tableView;
}


@end
